package com.example.uiclassassignments.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.uiclassassignments.tools.Intents;
import com.example.uiclassassignments.tools.ItemGriViewDate;
import com.example.uiclassassignments.tools.ItemGriviewMainAdapter;
import com.example.uiclassassignments.tools.ToastUtils;
import java.io.File;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
import static com.example.uiclassassignments.R.*;

public class MainActivity extends Activity implements View.OnClickListener {
    private static final int TAKE_PHOTO = 1;
    TextView btn_bd, btn_qq, btn_wy, btn_xl, btn_tm, btn_jd, btn_set, btn_exit, btn_zy;
    GridView gv_main;
    CircleImageView civ;
    SlidingPaneLayout slidingPaneLayout;
    private ArrayList<ItemGriViewDate> itemGriViewDates = new ArrayList<ItemGriViewDate>();
    public boolean ischeck = false;//用于判断作业按钮是否被按下
    private File picture;
    /* 头像文件 */
    private static final String IMAGE_FILE_NAME = "temp_head_image.jpg";
    /* 请求识别码 */
    private static final int CODE_GALLERY_REQUEST = 0xa0;
    private static final int CODE_CAMERA_REQUEST = 0xa1;
    private static final int CODE_RESULT_REQUEST = 0xa2;
    private Uri ImageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_main);
        initView();
        initData();
    }
    /*作业适配器及各个作业的点击事件*/
    public void Adapter() {
        ItemGriviewMainAdapter itemGriviewMainAdapter = new ItemGriviewMainAdapter(this, itemGriViewDates);
        gv_main.setAdapter(itemGriviewMainAdapter);
        gv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        Intents.Intent(MainActivity.this, RadioButtonActivity.class, null);
                        break;
                    case 1:
                        Intents.Intent(MainActivity.this, ProgressBarActivity.class, null);
                        break;
                    case 2:
                        Intents.Intent(MainActivity.this, SeekBarActivity.class, null);
                        break;
                    case 3:
                        Intents.Intent(MainActivity.this, CalendarViewActivity.class, null);
                        break;
                    case 4:
                        Intents.Intent(MainActivity.this, ImageSwitcherActivity.class, null);
                        break;
                    case 5:
                        Intents.Intent(MainActivity.this, ViewFlipperActivity.class, null);
                        break;
                    case 6:
                        Intents.Intent(MainActivity.this, FragmentViewPagerActivity.class, null);
                        break;
                    case 7:
                        Intents.Intent(MainActivity.this, ExpandableListViewActivity.class, null);
                        break;
                    case 8:
                        Intents.Intent(MainActivity.this, ListViewActivity.class, null);
                        break;
                    case 9:
                        Intents.Intent(MainActivity.this,GirdViewActivity.class,null);
                        break;
                    case 10:
                        Intents.Intent(MainActivity.this,SpinnerActivity.class,null);
                        break;
                    case 11:
                        Intents.Intent(MainActivity.this,DialogActivity.class,null);
                        break;
                    case 12:
                        Intents.Intent(MainActivity.this,ActionBarActivity.class,null);
                        break;
                    case 13:
                        Intents.Intent(MainActivity.this,PopupWindowActivity.class,null);
                        break;
                    case 14:
                        Intents.Intent(MainActivity.this,ContentMuneActivity.class,null);
                        break;
                    case 15:
                        Intents.Intent(MainActivity.this,OptionsMenuActivity.class,null);
                        break;
                    case 16:
                        Intents.Intent(MainActivity.this,SlidingPaneLayoutActivity.class,null);
                        break;
                    case 17:
                        Intents.Intent(MainActivity.this,LineChartActivity.class,null);
                }
            }
        });
    }
    /*作业适配器数据*/
    protected void initData() {
        itemGriViewDates = new ArrayList<ItemGriViewDate>();
        itemGriViewDates.add(new ItemGriViewDate("RadioButton"));
        itemGriViewDates.add(new ItemGriViewDate("ProgressBar"));
        itemGriViewDates.add(new ItemGriViewDate("SeeBar"));
        itemGriViewDates.add(new ItemGriViewDate("CalendarView"));
        itemGriViewDates.add(new ItemGriViewDate("ImageSwitcher"));
        itemGriViewDates.add(new ItemGriViewDate("ViewFlipper"));
        itemGriViewDates.add(new ItemGriViewDate("ViewPager+Fragment"));
        itemGriViewDates.add(new ItemGriViewDate("ExpandableListView"));
        itemGriViewDates.add(new ItemGriViewDate("ListView"));
        itemGriViewDates.add(new ItemGriViewDate("GridView"));
        itemGriViewDates.add(new ItemGriViewDate("Spinner"));
        itemGriViewDates.add(new ItemGriViewDate("Dialog"));
        itemGriViewDates.add(new ItemGriViewDate("AcitonBar"));
        itemGriViewDates.add(new ItemGriViewDate("PopupWindow"));
        itemGriViewDates.add(new ItemGriViewDate("ContextMenu"));
        itemGriViewDates.add(new ItemGriViewDate("optionsMenu"));
        itemGriViewDates.add(new ItemGriViewDate("SlidingPaneLayout"));
        itemGriViewDates.add(new ItemGriViewDate("LineChart"));
    }
    /*资源文件加载*/
    public void initView() {
        btn_bd = findViewById(id.btn_bd);
        btn_qq = findViewById(id.btn_qq);
        btn_wy = findViewById(id.btn_wy);
        btn_xl = findViewById(id.btn_xl);
        btn_tm = findViewById(id.btn_tm);
        btn_jd = findViewById(id.btn_jd);
        btn_set = findViewById(id.btn_set);
        btn_exit = findViewById(id.btn_exit);
        gv_main = findViewById(id.gv_main);
        btn_zy = findViewById(id.btn_zy);
        slidingPaneLayout = findViewById(id.sliding_layout);
        civ = findViewById(id.civ);

        btn_bd.setOnClickListener(this);
        btn_qq.setOnClickListener(this);
        btn_wy.setOnClickListener(this);
        btn_xl.setOnClickListener(this);
        btn_tm.setOnClickListener(this);
        btn_jd.setOnClickListener(this);
        btn_set.setOnClickListener(this);
        btn_exit.setOnClickListener(this);
        btn_zy.setOnClickListener(this);
        civ.setOnClickListener(this);
    }
    /*双重退出*/
    private long lostClickTime = 0;
    @Override
    public void onBackPressed() {
        //按键时间差
        if (lostClickTime <= 0) {
            ToastUtils.showTextToas(this, "再按一此退出应用");
            lostClickTime = System.currentTimeMillis();
        } else {
            long currentClickTime = System.currentTimeMillis();
            if (currentClickTime - lostClickTime < 1000) {
                finish();
                ToastUtils.showTextToas(this, "软件已退出");
            } else {
                lostClickTime = currentClickTime;
                ToastUtils.showTextToas(this, "再按一此退出应用");
            }
        }
    }
    /*侧拉点击*/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case id.btn_bd:
                Intents.IntentWeb(this, Uri.parse("https://www.baidu.com"));
                break;
            case id.btn_qq:
                Intents.IntentWeb(this, Uri.parse("https://www.qq.com"));
                break;
            case id.btn_wy:
                Intents.IntentWeb(this, Uri.parse("https://www.163.com"));
                break;
            case id.btn_xl:
                Intents.IntentWeb(this, Uri.parse("https://www.sina.com.cn"));
                break;
            case id.btn_tm:
                Intents.IntentWeb(this, Uri.parse("https://www.tianmao.com"));
                break;
            case id.btn_jd:
                Intents.IntentWeb(this, Uri.parse("http://www.jd.com"));
                break;
            case id.btn_set:
                Intent i = new Intent(Settings.ACTION_SETTINGS);
                startActivity(i);
                break;
            case id.btn_exit:
                onBackPressed();
                break;
            case id.btn_zy:
                Adapter();
                slidingPaneLayout.closePane();
                break;
            case id.civ:
                changeImage();
                break;
        }
    }
    /*侧拉图像点击选择*/
    public void changeImage() {
        final String[] font = {"拍照", "从图库选择"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setItems(font, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (font[which]) {
                    case "拍照":
                        ToastUtils.showTextToas(MainActivity.this,"相机不可用，请使用本地图片");
                        break;
                    case "从图库选择":
                        choseHeadImageFromGallery();
                        break;
                }
            }
        }).setNegativeButton("Cancle", null).setPositiveButton("OK", null).create().show();
    }
    // 从本地相册选取图片作为头像
    private void choseHeadImageFromGallery() {
        Intent intentFromGallery = new Intent();
        // 设置文件类型
        intentFromGallery.setType("image/*");
        intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // 用户没有进行有效的设置操作，返回
        if (resultCode == RESULT_CANCELED) {
            Toast.makeText(getApplication(), "取消", Toast.LENGTH_LONG).show();
            return;
        }
        switch (requestCode) {
            case CODE_GALLERY_REQUEST:
                cropRawPhoto(intent.getData());
                break;
            case CODE_CAMERA_REQUEST:
                if (hasSdcard()) {
                    File tempFile = new File(
                            Environment.getExternalStorageDirectory(),
                            IMAGE_FILE_NAME);
                    cropRawPhoto(Uri.fromFile(tempFile));
                } else {
                    Toast.makeText(getApplication(), "没有SDCard!", Toast.LENGTH_LONG)
                            .show();
                }
                break;
            case CODE_RESULT_REQUEST:
                if (intent != null) {
                    setImageToHeadView(intent);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, intent);
    }
    /**
     * 裁剪原始的图片
     */
    public void cropRawPhoto(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        // aspectX , aspectY :宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX , outputY : 裁剪图片宽高
        // 裁剪后图片的宽(X)和高(Y),480 X 480的正方形。
        int output_X = 480;
        intent.putExtra("outputX", output_X);
        int output_Y = 480;
        intent.putExtra("outputY", output_Y);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, CODE_RESULT_REQUEST);
    }
    /**
     * 提取保存裁剪之后的图片数据，并设置头像部分的View
     */
    private void setImageToHeadView(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            civ.setImageBitmap(photo);
        }
    }
    /**
     * 检查设备是否存在SDCard的工具方法
     */
    public static boolean hasSdcard() {
        String state = Environment.getExternalStorageState();
        // 有存储的SDCard
        return state.equals(Environment.MEDIA_MOUNTED);
    }

}
